package com.tarena.lbs.basic.web.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.basic.web.dao.repository.AdminRepository;
import com.tarena.lbs.basic.web.dao.repository.UserGroupRepository;
import com.tarena.lbs.basic.web.dao.repository.UserRespository;
import com.tarena.lbs.basic.web.dao.repository.UserTagsRepository;
import com.tarena.lbs.basic.web.service.UserService;
import com.tarena.lbs.common.basic.util.RandomUserName;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.basic.bo.AdminBO;
import com.tarena.lbs.pojo.basic.bo.UserBO;
import com.tarena.lbs.pojo.basic.bo.UserGroupBO;
import com.tarena.lbs.pojo.basic.dos.UserGroupDO;
import com.tarena.lbs.pojo.basic.param.UserGroupParam;
import com.tarena.lbs.pojo.basic.param.UserTagsParam;
import com.tarena.lbs.pojo.basic.query.UserGroupQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRespository userRespository;
    @Autowired
    private UserTagsRepository userTagsRepository;
    @Autowired
    private UserGroupRepository userGroupRepository;
    @Autowired
    private AdminRepository adminRepository;

    @Override
    public void saveUser(UserBO userBO) {
        //填补 默认status 0
        userBO.setStatus(0);
        userBO.setUserName(RandomUserName.createRandomName());
        userBO.setNickName(userBO.getUserName());
        userBO.setRegTime(new Date());
        userRespository.saveUser(userBO);
    }

    @Override
    public UserBO userDetail() throws BusinessException {
        //1.获取用户登录信息
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple ==null){
            throw new BusinessException("101","用户认证解析失败");
        }
        Integer userId = userPrinciple.getId();
        //2.调用餐厨层查询用户Id
        UserBO userBO = userRespository.getUserById(userId);
        return userBO;
    }

    /*
    * 业务流程
    * 1.判断用户是否登录成功
    *   1.1未登录成功,则throw Exception
    * 2.批量添加标数据 使用 ArrayList
    *   2.1 对标签进行拼接提取 (",")
    *   2.2 把userId 和 targetId 添加到 userTags 队列中
    * 3.先删除 user_tag表中的所有 与userId 相关联的数据,之后再进行Insert操作
    * 4.进行 insert 操作 ( 在 Repository层中 转化为Do对象 之后用 Foreach标签进行批量添加 )
    * 5.对用户表中的Status进行更新操作 (目的 当用户下次登录的时候不弹出那个选择标签的界面)
    * */
    @Override
    public void bindUserTags(UserTagsParam param) throws BusinessException {
        //1.判断用户是否登录
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("101","请登录用户,用户解析失败");
        }
        Integer userId = userPrinciple.getId();
        //2.处理标签数据
        List<UserTagsParam> userTags = new ArrayList<>();
        String[] tags = param.getTagIds().split(",");
        for (String tag : tags) {
            UserTagsParam userTagsParam = new UserTagsParam();
            userTagsParam.setUserId(userPrinciple.getId());
            userTagsParam.setTagIds(tag);
            userTags.add(userTagsParam);
        }
        //3.进行数据库的操作
        userTagsRepository.deleteUserTags(userId);
        userTagsRepository.batchInsertUserTags(userTags);
        //4.把用户的状态修改为1
        userRespository.updateUserStatus(userId,1);
    }

    @Override
    public BasePageList<UserGroupBO> userGroupPage(UserGroupQuery userGroupQuery) {
        return userGroupRepository.getUserGroupByPage(userGroupQuery);
    }

    @Override
    public void updateUserGroup(UserGroupBO param) throws  BusinessException{
        //1.判断用户是否登录
        UserPrinciple userPrinciple = LbsSecurityContenxt.getLoginToken();
        if (userPrinciple==null){
            throw new BusinessException("101","请登录用户,用户解析失败");
        }
        UserGroupDO userGroupDO = new UserGroupDO();
        //获取BusinessId
        Integer userId = userPrinciple.getId();
        AdminBO adminBO = adminRepository.getAdminById(userId);
        if (ObjectUtils.isEmpty(adminBO)){
            throw new BusinessException("102","商家不存在");
        }
        BeanUtils.copyProperties(param,userGroupDO);
        userGroupDO.setBusinessId(adminBO.getBusinessId());
        userGroupDO.setCreateAt(LocalDateTime.now());
        userGroupRepository.insertUserGroup(userGroupDO);
    }
}
